﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.Models;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS_Global;
using System.Globalization;
using System.Threading;

public partial class IFRS_WIN02_3V : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {            
            this.AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
            this.SetControl();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }    
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN02_1S.aspx");
    }
    #endregion
    #region "---------- Methods -----------"
    private void AddClientScript()
    {
        btnGLAccountCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");        
        btnCostCenter.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CostCenter.aspx") + "','700','550');");
        btnReferenceInterestCD.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/ReferenceInterest.aspx") + "','700','550');");
        btnCurrencyCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Currency.aspx") + "','700','550');");
        btnCustomer.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Customer.aspx") + "','700','550');");
        btnLoanObjective.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/VmLoanObjective.aspx") + "','700','550');");
       
        txtGLAccountCode.AddControl(hidGLAccountCode.ClientID);
        txtGLAccountCode.AddControl(txtGLAccountCode.ClientID);
        txtGLAccountCode.ControlSearch = txtGLAccountCode.ClientID;
        txtGLAccountCode.AddQueryString("bIsActive", "1");
        txtGLAccountCode.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName";

        txtCostCenter.AddControl(hidCostCenter.ClientID);
        txtCostCenter.AddControl(txtCostCenter.ClientID);
        txtCostCenter.ControlSearch = txtCostCenter.ClientID;
        txtCostCenter.AddQueryString("bIsActive", "1");
        txtCostCenter.SearchKey = "VWCostCenter_oCOST_CENTER_CD_oCOST_CENTER_NM";        

        txtReferenceInterestCD.AddControl(hidReferenceInterestCD.ClientID);
        txtReferenceInterestCD.AddControl(txtReferenceInterestCD.ClientID);
        txtReferenceInterestCD.ControlSearch = txtReferenceInterestCD.ClientID;
        txtReferenceInterestCD.SearchKey = "ReferenceInterest_oReferenceInterestCD_oReferenceInterestNM";

        txtCurrencyCode.AddControl(hidCurrencyCode.ClientID);
        txtCurrencyCode.AddControl(txtCurrencyCode.ClientID);
        txtCurrencyCode.ControlSearch = txtCurrencyCode.ClientID;
        txtCurrencyCode.AddQueryString("bIsActive", "1");
        txtCurrencyCode.SearchKey = "Currency_oCurrencyCode_oCurrencyName";
        
        txtCustomer.AddControl(hidCustomer.ClientID);
        txtCustomer.AddControl(txtCustomer.ClientID);
        txtCustomer.ControlSearch = txtCustomer.ClientID;
        txtCustomer.AddQueryString("bIsActive", "1");
        txtCustomer.SearchKey = "Customer_oCustomerID_oCustomerCode";
        
        txtLoanObjective.AddControl(hidLoanObjective.ClientID);
        txtLoanObjective.AddControl(txtLoanObjective.ClientID);
        txtLoanObjective.ControlSearch = txtLoanObjective.ClientID;
        txtLoanObjective.AddQueryString("bIsActive", "1");
        txtLoanObjective.SearchKey = "VmLoanObjective_oCBT_CUST_BUSINESS_TYPE_ID_oCBT_DESC_LEV1";
    }
    private void BindData(int ID)
    {
        List<ManualLoan> manualLoan = new List<ManualLoan>();
        DataTable dt = new SPR_MANUAL_LOAN().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, manualLoan);

            txtTransactionDate.Text = manualLoan[0].TransactionDate.DateToString();
            txtAccountNo.Text = manualLoan[0].AccountNo;
            txtGLAccountCode.Text = manualLoan[0].GLAccount;
            txtCostCenter.Text = manualLoan[0].CostCenterName;           
            txtStartDate.Text = manualLoan[0].StartedDate.DateToString();
            txtEndDate.Text = manualLoan[0].EndDate.DateToString();
            txtLastPayDate.Text = manualLoan[0].LastPayDate.DateToString();
            txtNextPayDate.Text = manualLoan[0].NextPayDate.DateToString();
            txtPaymentFreq.Text = manualLoan[0].PaymentFreq.ToString();
            ddlPaymentFreqTimeBase.SelectedValue = manualLoan[0].PaymentFreqTimeBase;
            ddlPaymentCondition.SelectedValue = manualLoan[0].PaymentConditionID.ToString();
            ddlCalInterestCond.Text = manualLoan[0].CalInterestCondID.ToString();
            ddlInterestType.Text = manualLoan[0].InterestTypeID.ToString();
            txtInterestRate.Text = manualLoan[0].InterestRate.ToString();
            txtReferenceInterestCD.Text = manualLoan[0].RefInterestName;
            txtAdditionalInterest.Text = manualLoan[0].AdditionalInterest.ToString();
            ddlDayCountMethod.Text = manualLoan[0].DayCountMethodID.ToString();
            txtCurrencyCode.Text = manualLoan[0].CurrencyCode;
            txtUnpaidInterest.Text = manualLoan[0].UnpaidInterest.ToString();
            txtDept.Text = manualLoan[0].Dept.ToString();
            ddlCreditLevel.Text = manualLoan[0].CreditLevelID.ToString();
            txtCustomer.Text = manualLoan[0].CustomerCode;
            ddlContractLoanType.SelectedValue = manualLoan[0].ContractLoanTypeID.ToString();
            txtLoanObjective.Text = manualLoan[0].LoanObjectiveName;
            txtFirstMisDate.Text = manualLoan[0].FisrtMisDate.DateToString();
            chkAdjustFlag.Checked = manualLoan[0].AdjustFlag == "Y" ? true : false;
            txtReservedAmount.Text = manualLoan[0].ReservedAmount.ToString();
            txtApprovedAmount.Text = manualLoan[0].ApprovedAmount.ToString();
            txtStatus.Text = manualLoan[0].IsConfirmed == false ? "สร้างรายการ" : "ยืนยัน";        
        }
    }
    private void BindDDL()
    {
        ddlPaymentFreqTimeBase.SetData(DDL_TYPE.PaymentFrequencyTimeBase, "", false, true);
        ddlPaymentCondition.SetData(DDL_TYPE.PaymentCondition, "", false, true);
        ddlCalInterestCond.SetData(DDL_TYPE.CalInterestCond, "", false, true);
        ddlInterestType.SetData(DDL_TYPE.InterestType, "", false, true);
        ddlDayCountMethod.SetData(DDL_TYPE.DayCountMethod, "", false, true);
        ddlCreditLevel.SetData(DDL_TYPE.CreditLevel, "", false, true);
        ddlContractLoanType.SetData(DDL_TYPE.ContractLoanType, "", false, true);
        txtStatus.Text = "สร้างรายการ";
    }
    private void SetControl()
    {
        SetControlReadOnly(form_control);
    }
    protected void SetControlReadOnly(PlaceHolder plcReadOnly)
    {
        foreach (Control ctrl in plcReadOnly.Controls)
        {
            if (ctrl is Button)
            {
                ((Button)ctrl).Enabled = false;
            }
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Attributes["readOnly"] = "true";
                ((TextBox)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is DropDownList)
            {
                ((DropDownList)ctrl).Enabled = false;
                ((DropDownList)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is RadioButton)
            {
                ((RadioButton)ctrl).Attributes["readOnly"] = "true";
                ((RadioButton)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is CheckBox)
            {
                ((CheckBox)ctrl).Enabled = false;
                ((CheckBox)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is Image)
            {
                ((Image)ctrl).Attributes["disabled"] = "true";
            }
        }
    }
    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN02_3V;
    }
    #endregion
}